package util;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import hotel.entity.ItemDescription;
import hotel.entity.Reserve;
import hotel.entity.Sales;
import hotel.entity.SalesLineItem;

public class JSONUtil {

	/**
	 * 返回成功JSON数据，状态码200，信息'保存成功'
	 */
	public static String returnSuccess() {
		return "{\"statusCode\":\"200\",\"message\":\"保存成功\"}";
	}
	
	/**
	 * 返回成功JSON数据，状态码200，信息自定义
	 */
	public static String returnSuccess(String msg) {
		return "{\"statusCode\":\"200\",\"message\":\""+msg+"\"}";
	}
	
	/**
	 * 返回失败JSON数据，状态码300，信息'操作失败'
	 */
	public static String returnFailure() {
		return "{\"statusCode\":\"300\",\"message\":\"操作失败\"}";
	}
	
	/**
	 * 因为layui的数据表格不支持xxx.xxx这种使用方式，因此不能使用Jackson工具转换成JSON数据
	 * 所以手动转换
	 * 将预定列表转换成JSON数据格式
	 */
	public static String reserveListToJson(List<Reserve> list) {
		if(list==null||list.size()<=0)
			return "[]";
		StringBuffer buffer=new StringBuffer();
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		buffer.append('[');
		for(Reserve r:list) {
			buffer.append("{");
			buffer.append("\"reserveid\":\""+r.getReserveid()+"\",");
			buffer.append("\"categoryName\":\""+r.getCategory().getCategoryName()+"\",");
			buffer.append("\"amount\":\""+r.getAmount()+"\",");
			buffer.append("\"days\":\""+r.getDays()+"\",");
			buffer.append("\"start_time\":\""+format.format(r.getStart_time())+"\",");
			buffer.append("\"end_time\":\""+format.format(r.getEnd_time())+"\",");
			buffer.append("\"userName\":\""+r.getUserName()+"\",");
			buffer.append("\"tele\":\""+r.getTele()+"\",");
			//buffer.append("\"id_number\":\""+r.getId_number()+"\",");
			buffer.append("\"operateUserId\":\""+r.getOperateUser().getUserid()+"\",");
			buffer.append("\"reserveTime\":\""+format.format(r.getReserveTime())+"\",");
			buffer.append("\"state\":\""+r.getState()+"\"");
			buffer.append("},");
		}
		buffer.deleteCharAt(buffer.length()-1);
		buffer.append(']');
		return buffer.toString();
	}
	
	/**
	 * 将订单项详情转换成JSON数据格式
	 */
	public static String checkinListToJson(List<SalesLineItem> list) {
		if(list==null||list.size()<=0)
			return "[]";
		StringBuffer temp=new StringBuffer();
		StringBuffer buffer=new StringBuffer();
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		buffer.append('[');
		for(SalesLineItem s:list) {
			buffer.append("{");
			buffer.append("\"itemid\":\""+s.getItemid()+"\",");
			buffer.append("\"orderNumber\":\""+s.getSales().getOrderNumber()+"\",");
			buffer.append("\"categoryName\":\""+s.getSales().getCategory().getCategoryName()+"\",");
			buffer.append("\"roomNo\":\""+s.getRoom().getNo()+"\",");
			buffer.append("\"count\":\""+s.getCount()+"\",");
			temp=new StringBuffer();
			for(ItemDescription desc:s.getDescriptions()) {
				temp.append(", "+desc.getCustomerName());
			}
			buffer.append("\"customerName\":\""+temp.substring(1)+"\",");
			buffer.append("\"checkin_time\":\""+format.format(s.getCheckin_time())+"\",");
			if(s.getCheckout_time()!=null)
				buffer.append("\"checkout_time\":\""+format.format(s.getCheckout_time())+"\",");
			else
				buffer.append("\"checkout_time\":\"\",");
			buffer.append("\"operateUserId\":\""+s.getSales().getOperateUser().getUserid()+"\",");
			buffer.append("\"state\":\""+s.getState()+"\"");
			buffer.append("},");
		}
		buffer.deleteCharAt(buffer.length()-1);
		buffer.append(']');
		return buffer.toString();
	}
	
	public static String salesListToJson(List<Sales> list) {
		if(list==null||list.size()<=0)
			return "[]";
		StringBuffer buffer=new StringBuffer();
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		buffer.append('[');
		for(Sales s:list) {
			buffer.append("{");
			buffer.append("\"salesid\":\""+s.getSalesid()+"\",");
			buffer.append("\"orderNumber\":\""+s.getOrderNumber()+"\",");
			buffer.append("\"operateUserid\":\""+s.getOperateUser().getUserid()+"\",");
			buffer.append("\"categoryName\":\""+s.getCategory().getCategoryName()+"\",");
			buffer.append("\"amount\":\""+s.getAmount()+"\",");
			buffer.append("\"totalPrice\":\""+s.getTotalPrice()+"\",");
			buffer.append("\"salesTime\":\""+format.format(s.getSalesTime())+"\"");
			buffer.append("},");
		}
		buffer.deleteCharAt(buffer.length()-1);
		buffer.append(']');
		return buffer.toString();
	}
	
	/**
	 * 将房间统计信息转换成JSON格式供Highcharts使用
	 * （首页饼状图）
	 */
	public static String roomStatisticsToJson(Map<String,Double> map) {
		if(map==null||map.size()<1)
			return "[]";
		StringBuffer buffer=new StringBuffer();
		//buffer.append("[");
		for(Map.Entry<String, Double> entry:map.entrySet()) {
			buffer.append("{");
			buffer.append("\"name\":\""+entry.getKey()+"\",");
			buffer.append("y:"+entry.getValue()+"");
			buffer.append("},");
		}
		buffer.deleteCharAt(buffer.length()-1);
		//buffer.append("]");
		return buffer.toString();
	}
	
}
